package com.blame.springblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blame.springblog.common.exception.BlogException;
import com.blame.springblog.common.utils.BeanTranUtils;
import com.blame.springblog.constant.Constants;
import com.blame.springblog.mapper.BlogInfoMapper;
import com.blame.springblog.pojo.dataobject.BlogInfo;
import com.blame.springblog.pojo.request.AddBlogRequest;
import com.blame.springblog.pojo.request.UpdateBlogRequest;
import com.blame.springblog.pojo.response.BlogInfoResponse;
import com.blame.springblog.pojo.response.UserInfoResponse;
import com.blame.springblog.service.BlogInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.stream.IIOByteBuffer;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class BlogInfoServiceImpl implements BlogInfoService {

    @Autowired
    private BlogInfoMapper blogInfoMapper;

    public List<BlogInfoResponse> getList() {

        List<BlogInfo> blogInfos=blogInfoMapper.selectList( //mybatis-plus的查询方法，用户查询满足以下条件的所有记录
                new LambdaQueryWrapper<BlogInfo>()  //创建一个条件构造器
                .eq(BlogInfo::getDeleteFlag, Constants.FLAG_NORMAL).orderByDesc(BlogInfo::getId));  //.eq：条件是
                                                                                    //orderByDesc是将得到的id按desc(降序)排序
        //转化格式将BlogInfo格式转化为BlogInfoResponse
        return blogInfos.stream().map(blogInfo ->{   //.stream()将得到的博客列表变成一个数据流， .map对每一个数据进行转换
            BlogInfoResponse response=new BlogInfoResponse();  //创建一个新的对象
            BeanUtils.copyProperties(blogInfo,response);  //将blogInfo中的数据复制到response中，
            return response;                              // 这里的blogInfo指的是List<BlogInfo> blogInfos中的每一个数据
        }).collect(Collectors.toList());  //最后再将将 Stream 流结果收集成 List
    }

    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {

        return BeanTranUtils.tran(getBlogInfo(blogId));
    }

    @Override
    public BlogInfo getBlogInfo(Integer blogId) {
        QueryWrapper<BlogInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(BlogInfo::getDeleteFlag, Constants.FLAG_NORMAL)
                .eq(BlogInfo::getId, blogId);
        return blogInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public Boolean addBlog(AddBlogRequest addBlogRequest) {
        BlogInfo blogInfo=new BlogInfo();
        BeanUtils.copyProperties(addBlogRequest,blogInfo);
        try {
            int result=blogInfoMapper.insert(blogInfo);
            if(result==1){
                return true;
            }
            return false;
        }catch (Exception e){
            log.error("发布错误,请联系管理员");
            throw new BlogException("发布错误，请联系管理员");
        }
    }

    @Override
    public Boolean updateBlog(UpdateBlogRequest updateBlogRequest) {
        BlogInfo blogInfo=BeanTranUtils.tran(updateBlogRequest);

        try {
            int result = blogInfoMapper.updateById(blogInfo);
            return result==1;
        }catch (Exception e){
            log.error("编辑失败");
            throw new BlogException("内部错误，请联系管理员");
        }

    }

    @Override
    public Boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo=new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(Constants.FLAG_DELETE);
        try {
            int result = blogInfoMapper.updateById(blogInfo);
            return result==1;
        }catch (Exception e){
            log.error("删除失败");
            throw new BlogException("内部错误，请联系管理员");
        }

    }


}
